package com.hue.springbootvalidation.validation;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.HashSet;
import java.util.Set;

/**
 * 自定义性别效验器，被效验的参数只能是有限个值
 * 自定义效验器需要实现ConstraintValidator接口，泛型为对应的参数效验注解和参数的类型
 */
public class SexValidation implements ConstraintValidator<SexValue, String> {
    private Set<String> values = new HashSet<>();

    @Override
    public void initialize(SexValue constraintAnnotation) {
        values.add("男");
        values.add("女");
    }

    @Override
    public boolean isValid(String s, ConstraintValidatorContext constraintValidatorContext) {
        return values.contains(s);
    }
}
